import re

# 检查是否存在 NULL 的使用
def has_null_usage(content):
    pattern = r'\bNULL\b'
    return re.search(pattern, content)

# 检查是否存在 #define NULL ((void*)0)
def has_null_definition(content):
    pattern = r'^\s*#define\s+NULL\s+\(\(void\*\)\s*0\)\s*$'
    return re.search(pattern, content, re.MULTILINE) is not None

# 创建包含 #define NULL ((void*)0) 的同名文件
def insert_null_definition(file_path):
    with open(file_path, 'r') as file:
        content = file.read()

    if has_null_usage(content) and not has_null_definition(content):
        # insert #define NULL ((void*)0)
        content = '#define NULL ((void*)0)\n' + content

        # write extended file content into new file
        with open(file_path, "w") as extended_file:
            extended_file.write(content)
